/*
package com.example.demo.listToTree;


import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ZoneUtils {

    */
/**
     * 方法一：两层循环
     *
     * @param zoneList
     * @return
     *//*

   */
/* public static List<Zone> buildTree2(List<Zone> zoneList) {
        List<Zone> result = new ArrayList<>();
        for (Zone zone : zoneList) {
            if (zone.parentId.equals("0")) {
                result.add(zone);
            }
            for (Zone child : zoneList) {
                if (child.parentId.equals(zone.id)) {
                    zone.addChildren(child);
                }
            }
        }
        return result;
    }*//*


    */
/**
     * 方法二 ：两次遍历
     * 推荐使用方法二
     *
     * @param zoneList
     * @return
     *//*

    public static List<Zone> buildTree3(List<Zone> zoneList) {
        Map<String, List<Zone>> zoneByParentIdMap = new HashMap<>();
        zoneList.forEach(zone -> {
            List<Zone> children = zoneByParentIdMap.getOrDefault(zone.parentId, new ArrayList<>());
            children.add(zone);
            zoneByParentIdMap.put(zone.parentId, children);
        });
        zoneList.forEach(zone -> zone.children = zoneByParentIdMap.get(zone.id));
        return zoneList.stream()
                .filter(v -> v.parentId.equals("0"))
                .collect(Collectors.toList());
    }

    */
/**
     * 方法二 ：使用java8的stream的方式
     * 推荐使用方法二
     *
     * @param zoneList
     * @return
     *//*

    public static List<Zone> buildTree3_01(List<Zone> zoneList) {
        Map<String, List<Zone>> zoneByParentIdMap = zoneList.stream().collect(Collectors.groupingBy(Zone::getParentId));
        zoneList.forEach(zone -> zone.children = zoneByParentIdMap.get(zone.getId()));
        return zoneList.stream().filter(v -> v.parentId.equals("0")).collect(Collectors.toList());
    }


    public static void test(List<?> zoneList) {
        for (int i = 0; i < zoneList.size(); i++) {
            Object o = zoneList.get(i);
            System.out.println(JSONObject.toJSONString(o));
        }
     }
}*/
